//
//  DateViewController.m
//  geiTA
//
//  Created by 曲丹阳 on 14-11-19.
//  Copyright (c) 2014年 lanou3g.com 蓝鸥科技. All rights reserved.
//

#import "DateViewController.h"
#import "AddViewController.h"
#import "DatedetailsViewController.h"
#import "DateCell.h"
#import "RecordDataBase.h"
#import "RecordModel.h"
#import "POAPinyin.h"
#import "DWBubbleMenuButton.h"
#import "mySinglietongClass.h"
#import "TheCountdown.h"
@interface DateViewController ()<UITableViewDataSource, UITableViewDelegate>

@end

@implementation DateViewController

//试图将要出现时
- (void)viewWillAppear:(BOOL)animated
{
    self.array =[NSMutableArray array];
    self.arr = [RecordDataBase selectAll];
    if ([_arr count ] == 0) {
        NSMutableDictionary *dic = [NSMutableDictionary dictionary];
        //向字典添加分组
        [dic setObject:@"" forKey:@"FzName"];
        [dic setObject:_arr forKey:@"array"];
        
        [_array addObject:dic];
        
    }else{
    
    
    self.share  = [mySinglietongClass shareManager];
    /********************  默认排序  *********************/
    if (_share.FZ == 3) {
    
        NSMutableDictionary *dic = [NSMutableDictionary dictionary];
        //向字典添加分组
        [dic setObject:@"默认" forKey:@"FzName"];
        [dic setObject:_arr forKey:@"array"];

        [_array addObject:dic];
        
        
        for (int i = 0; i <[_arr count]-1; i ++) {
            for (int j = 0; j < [_arr count]-i- 1;  j++) {
                RecordModel *model = [_arr objectAtIndex:j];
                NSString *str = [TheCountdown theCountWith:model.remindDate];
                NSArray *rr = [str componentsSeparatedByString:@"天"];
                NSString *st = [rr objectAtIndex:0];
                NSInteger s =[st integerValue];
                RecordModel *model1 = [_arr objectAtIndex:j+1];
                NSString *str1 = [TheCountdown theCountWith:model.remindDate];
                NSArray *rr1 = [str1 componentsSeparatedByString:@"天"];
                NSString *st1 = [rr1 objectAtIndex:0];
                NSInteger s1 = [st1 integerValue];
    
                if ( s  > s1 ) {
                    RecordModel*temp = [[RecordModel alloc]init];
                    
                    temp.recordid = model.recordid;
                    temp.name = model.name;
                    temp.sex =model.sex;
                    temp.remindDate = model.remindDate;
                    temp.phone = model.phone;
                    temp.relationship = model.relationship;
                    temp.note =model.note;
                    
                   
                    model.recordid = model1.recordid;
                    model.name = model1.name;
                    model.sex =model1.sex;
                    model.remindDate = model1.remindDate;
                    model.phone = model1.phone;
                    model.relationship = model1.relationship;
                    model.note =model1.note;
                    
                   
                    model1.recordid = temp.recordid;
                    model1.name = temp.name;
                    model1.sex = model1.sex;
                    model1.remindDate = temp.remindDate;
                    model1.phone = temp.phone;
                    model1.relationship = temp.relationship;
                    model1.note = temp.note;
                    
                    
                }
            }
        }
        

        
    }

    
    /**************** 姓名排序    ******************/
    if (_share.FZ == 1) {
        for (RecordModel *model in _arr) {
            BOOL  is = YES;
            
            NSString *str = [POAPinyin Convert:model.name];
            NSString *headLetter  = [str substringToIndex:1];//首字母
            char c = [headLetter characterAtIndex:0];
            NSLog(@"%c",c);
            
            if (c <65 && c > 90) {
                headLetter = @"#";
            }
            
            for (NSMutableDictionary*dic in _array) {
                
                if ([headLetter isEqualToString:[dic objectForKey:@"FzName"]]) {
                    NSMutableArray *array = [dic objectForKey:@"array"];
                    [array addObject:model];
                    
                    is  = NO;
                }
            }
            
            if (is == YES) {
                NSMutableDictionary *dic = [NSMutableDictionary dictionary];
                //向字典添加分组
                [dic setObject:headLetter forKey:@"FzName"];
                NSMutableArray *array  = [NSMutableArray array];
                [dic setObject:array forKey:@"array"];
                [array addObject:model];
                [_array addObject:dic];
            }
            //重新排序
            NSArray *sortDescriptors = [NSArray arrayWithObject:[NSSortDescriptor sortDescriptorWithKey:@"FzName" ascending:YES]];
            [_array sortUsingDescriptors:sortDescriptors];
            
            
            
        }
        
    }

    
    /**************** 月份排序    ******************/
    else if (_share.FZ == 2){
        for (RecordModel *model in _arr) {
            BOOL  is = YES;
            NSString *headLetter  = [model.remindDate substringWithRange:NSMakeRange(5, 3)];//首字母

            for (NSMutableDictionary*dic in _array) {

                if ([headLetter isEqualToString:[dic objectForKey:@"FzName"]]) {
                    NSMutableArray *array = [dic objectForKey:@"array"];
                    [array addObject:model];
                    is  = NO;
                    
                }
            }

            
            if (is == YES) {
            
                NSMutableDictionary *dic = [NSMutableDictionary dictionary];
                //向字典添加分组
                [dic setObject:headLetter forKey:@"FzName"];
                NSMutableArray *array  = [NSMutableArray array];
                [dic setObject:array forKey:@"array"];
                [array addObject:model];
                [_array addObject:dic];
            
            }
            //重新排序
            NSArray *sortDescriptors = [NSArray arrayWithObject:[NSSortDescriptor sortDescriptorWithKey:@"FzName" ascending:YES]];
            [_array sortUsingDescriptors:sortDescriptors];
   
        }
    }
    
    [self.table reloadData];
    }
}



- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationController.navigationBar.translucent =NO;
    [self.view setBackgroundColor:[UIColor yellowColor]];

    [self createTableView];
    [self createBarButton];
  
    
}


#pragma mark -
#pragma mark barButton
- (void)createBarButton
{
    UIButton *button = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 50, 50)];
    [button setTitle:@"添加" forState:UIControlStateNormal];
    [button setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
    [button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
    
    UIBarButtonItem *barbutton = [[UIBarButtonItem alloc]initWithCustomView:button];
    
    
    
    [self.navigationItem setRightBarButtonItem:barbutton];
    
    DWBubbleMenuButton *view = [[DWBubbleMenuButton alloc]initWithFrame:CGRectMake(0, 0, 40, 40) expansionDirection:DirectionRight];
    
    UIView *view1 = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 40, 40)];
    [view1 setBackgroundColor:[UIColor redColor]];
    [view setHomeButtonView:view1];
    
    
    //循环 创建下拉的button
    NSMutableArray *buttonsMutable = [[NSMutableArray alloc] init];
    
    int i = 111115;
    for (NSString *title in @[@"默认",@"姓名", @"月份" ]) {
        UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
        
        [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [button setTitle:title forState:UIControlStateNormal];
        
        button.frame = CGRectMake(0.f, 0.f, 30.f, 30.f);
        button.layer.cornerRadius = button.frame.size.height / 2.f;
        button.backgroundColor = [UIColor colorWithRed:0.f green:0.f blue:0.f alpha:0.5f];
        button.clipsToBounds = YES;
        button.tag = i++;
        
        [button addTarget:self action:@selector(test:) forControlEvents:UIControlEventTouchUpInside];
        
        [buttonsMutable addObject:button];
    }
    [view addButtons:buttonsMutable];
    

    
    UIBarButtonItem *barbutton1 = [[UIBarButtonItem alloc]initWithCustomView:view];
    [self.navigationItem setLeftBarButtonItem:barbutton1];
    
    
    
    
    
    
    
    
    [button release];
    [barbutton release];
    
}
- (void)test:(UIButton *)sender
{
    if (sender.tag == 111116) {
        _share.FZ = 1;
        [_array removeAllObjects]; //removeAllobjects   清空数组
        self.arr = [RecordDataBase selectAll];
        for (RecordModel *model in _arr) {
            BOOL  is = YES;
            
            NSString *str = [POAPinyin Convert:model.name];
            NSString *headLetter  = [str substringToIndex:1];//首字母
            char c = [headLetter characterAtIndex:0];
            NSLog(@"%c",c);
            
            if (c >64 && c < 91) {
                
            }else
            {
                headLetter = @"#";
            }
            
            
            for (NSMutableDictionary*dic in _array) {
                
                if ([headLetter isEqualToString:[dic objectForKey:@"FzName"]]) {
                    NSMutableArray *array = [dic objectForKey:@"array"];
                    [array addObject:model];
                    
                    is  = NO;
                }
            }
            
            if (is == YES) {
                NSMutableDictionary *dic = [NSMutableDictionary dictionary];
                //向字典添加分组
                [dic setObject:headLetter forKey:@"FzName"];
                NSMutableArray *array  = [NSMutableArray array];
                [dic setObject:array forKey:@"array"];
                [array addObject:model];
                [_array addObject:dic];
            }
            //重新排序
            NSArray *sortDescriptors = [NSArray arrayWithObject:[NSSortDescriptor sortDescriptorWithKey:@"FzName" ascending:YES]];
            [_array sortUsingDescriptors:sortDescriptors];

        }

    }
    else if(sender.tag == 111117){
        _share.FZ = 2;
        [_array removeAllObjects];
        self.arr = [RecordDataBase selectAll];
        for (RecordModel *model in _arr) {
            BOOL  is = YES;
            
            
            NSString *headLetter  = [model.remindDate substringWithRange:NSMakeRange(5, 3)];//首字母
            
            
            
            for (NSMutableDictionary*dic in _array) {
                
                if ([headLetter isEqualToString:[dic objectForKey:@"FzName"]]) {
                    NSMutableArray *array = [dic objectForKey:@"array"];
                    [array addObject:model];
                    
                    is  = NO;
                }
            }
            
            if (is == YES) {
                NSMutableDictionary *dic = [NSMutableDictionary dictionary];
                //向字典添加分组
                [dic setObject:headLetter forKey:@"FzName"];
                NSMutableArray *array  = [NSMutableArray array];
                [dic setObject:array forKey:@"array"];
                [array addObject:model];
                [_array addObject:dic];
            }
            //重新排序
            NSArray *sortDescriptors = [NSArray arrayWithObject:[NSSortDescriptor sortDescriptorWithKey:@"FzName" ascending:YES]];
            [_array sortUsingDescriptors:sortDescriptors];
        }
        
        
    }
    else
    {
        _share.FZ = 3;
            NSMutableDictionary *dic = [NSMutableDictionary dictionary];
            //向字典添加分组
            [dic setObject:@"默认" forKey:@"FzName"];
            [dic setObject:_arr forKey:@"array"];
            [_array removeAllObjects];
            [_array addObject:dic];
            
            
            for (int i = 0; i <[_arr count]-1; i ++) {
                for (int j = 0; j < [_arr count]-i- 1;  j++) {
                    RecordModel *model = [_arr objectAtIndex:j];
                    NSString *str = [TheCountdown theCountWith:model.remindDate ];
                    NSArray *rr = [str componentsSeparatedByString:@"天"];
                    NSString *st = [rr objectAtIndex:0];
                    NSInteger s =[st integerValue];
                    RecordModel *model1 = [_arr objectAtIndex:j+1];
                    NSString *str1 = [TheCountdown theCountWith:model1.remindDate ];
                    NSArray *rr1 = [str1 componentsSeparatedByString:@"天"];
                    NSString *st1 = [rr1 objectAtIndex:0];
                    NSInteger s1 = [st1 integerValue];
                    
                    if ( s  > s1 ) {
                        RecordModel*temp = [[RecordModel alloc]init];
                        
                        temp.recordid = model.recordid;
                        temp.name = model.name;
                        temp.sex =model.sex;
                        temp.remindDate = model.remindDate;
                        temp.phone = model.phone;
                        temp.relationship = model.relationship;
                        temp.note =model.note;
                        
                        
                        model.recordid = model1.recordid;
                        model.name = model1.name;
                        model.sex =model1.sex;
                        model.remindDate = model1.remindDate;
                        model.phone = model1.phone;
                        model.relationship = model1.relationship;
                        model.note =model1.note;
                        
                        
                        model1.recordid = temp.recordid;
                        model1.name = temp.name;
                        model1.sex = model1.sex;
                        model1.remindDate = temp.remindDate;
                        model1.phone = temp.phone;
                        model1.relationship = temp.relationship;
                        model1.note = temp.note;
                        
                        
                    }
                }
            }
    }
    [self.table reloadData];

}



- (void)buttonAction:(id)sender
{
    _share.selectPage = 1;
    AddViewController *add = [[AddViewController alloc]init];
    [self.navigationController pushViewController:add animated:YES];
}



#pragma mark -
#pragma mark TableView
- (void)createTableView
{
    self.table = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, self.view.self.frame.size.width, self.view.frame.size.height-113) style:UITableViewStyleGrouped];
    [_table setBackgroundColor:[UIColor yellowColor]];
    _table.rowHeight = 80;
    _table.delegate = self;
    _table.dataSource = self;
    [self.view addSubview:_table];
    
    
}

//senction
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return [_array count];
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    NSMutableDictionary *dic = [_array objectAtIndex:section];
    
    return [dic objectForKey:@"FzName"];
}

//cell
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    NSMutableDictionary *dic = [_array objectAtIndex:section];
    
    return [[dic objectForKey:@"array"]count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;
{
    static NSString *pool =@"ok";
    DateCell *cell =[tableView dequeueReusableCellWithIdentifier:pool];
    if (!cell) {
        cell = [[DateCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:pool];
    }
    NSMutableDictionary *dic = [_array objectAtIndex:indexPath.section];
    NSMutableArray *arr = [dic objectForKey:@"array"];
    RecordModel *model1 = [arr objectAtIndex:indexPath.row];
    DateModel *model = [[DateModel alloc]init];
    model.name =model1.name;
    model.remind = model1.remindDate;
    NSString *str = [TheCountdown theCountWith:model1.remindDate];
    model.countd =str;
    //获取图片路径
    NSString *fullPath = [[NSHomeDirectory() stringByAppendingPathComponent:@"Documents"] stringByAppendingPathComponent:[NSString stringWithFormat:@"%ld.png",(long)model1.recordid]];
    UIImage *savedImage = [[UIImage alloc] initWithContentsOfFile:fullPath];
    [cell.photoImageView setImage:savedImage];
    cell.model = model;
    
    
    return cell;
}

//cell跳转方法
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    DatedetailsViewController *derails = [[DatedetailsViewController alloc]init];
    [self.navigationController pushViewController:derails animated:YES];
    
    NSMutableDictionary *dic = [_array objectAtIndex:indexPath.section];
    NSMutableArray *arr = [dic objectForKey:@"array"];
    RecordModel *model = [arr objectAtIndex:indexPath.row];
    
    [derails  chuangzhi:model];

    
    
}

#pragma mark - 
#pragma mark 删除
-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSMutableDictionary *dic = [_array objectAtIndex:indexPath.section];
    NSMutableArray *arr = [dic objectForKey:@"array"];
    RecordModel *model = [arr objectAtIndex:indexPath.row];
    
    [RecordDataBase deleteWithModel:model.recordid];
    
    [_array removeAllObjects];
    self.arr = [RecordDataBase selectAll];
    if (_share.FZ != 3) {
        for (RecordModel *model in _arr) {
            BOOL  is = YES;
            
            NSString *str = [POAPinyin Convert:model.name];
            NSString *headLetter  = [str substringToIndex:1];//首字母
            char c = [headLetter characterAtIndex:0];
            NSLog(@"%c",c);
            
            if (c >64 && c < 91) {
                
            }else
            {
                headLetter = @"#";
            }
            
            
            for (NSMutableDictionary*dic in _array) {
                
                if ([headLetter isEqualToString:[dic objectForKey:@"FzName"]]) {
                    NSMutableArray *array = [dic objectForKey:@"array"];
                    [array addObject:model];
                    
                    is  = NO;
                }
            }
            
            if (is == YES) {
                NSMutableDictionary *dic = [NSMutableDictionary dictionary];
                //向字典添加分组
                [dic setObject:headLetter forKey:@"FzName"];
                NSMutableArray *array  = [NSMutableArray array];
                [dic setObject:array forKey:@"array"];
                [array addObject:model];
                [_array addObject:dic];
            }
            //重新排序
            NSArray *sortDescriptors = [NSArray arrayWithObject:[NSSortDescriptor sortDescriptorWithKey:@"FzName" ascending:YES]];
            [_array sortUsingDescriptors:sortDescriptors];
            
            
            
        }

    }else
    {
        
            NSMutableDictionary *dic = [NSMutableDictionary dictionary];
            //向字典添加分组
            [dic setObject:@"默认" forKey:@"FzName"];
            [dic setObject:_arr forKey:@"array"];
            
            [_array addObject:dic];
            
            
            for (int i = 0; i <[_arr count]-1; i ++) {
                for (int j = 0; j < [_arr count]-i- 1;  j++) {
                    RecordModel *model = [_arr objectAtIndex:j];
                    NSString *str = [TheCountdown theCountWith:model.remindDate ];
                    NSArray *rr = [str componentsSeparatedByString:@"天"];
                    NSString *st = [rr objectAtIndex:0];
                    NSInteger s =[st integerValue];
          
                    RecordModel *model1 = [_arr objectAtIndex:j+1];
                    NSString *str1 = [TheCountdown theCountWith:model1.remindDate ];
                    NSArray *rr1 = [str1 componentsSeparatedByString:@"天"];
                    NSString *st1 = [rr1 objectAtIndex:0];
                    NSInteger s1 = [st1 integerValue];
                    
                    if ( s  > s1 ) {
                        RecordModel*temp = [[RecordModel alloc]init];
                        
                        temp.recordid = model.recordid;
                        temp.name = model.name;
                        temp.sex =model.sex;
                        temp.remindDate = model.remindDate;
                        temp.phone = model.phone;
                        temp.relationship = model.relationship;
                        temp.note =model.note;
                        
                        
                        model.recordid = model1.recordid;
                        model.name = model1.name;
                        model.sex =model1.sex;
                        model.remindDate = model1.remindDate;
                        model.phone = model1.phone;
                        model.relationship = model1.relationship;
                        model.note =model1.note;
                        
                        
                        model1.recordid = temp.recordid;
                        model1.name = temp.name;
                        model1.sex = model1.sex;
                        model1.remindDate = temp.remindDate;
                        model1.phone = temp.phone;
                        model1.relationship = temp.relationship;
                        model1.note = temp.note;
                        
                        
                    }
                }
            }
            
            
            
        }
  
    

    
    


    [_table reloadData];
}








@end
